﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class DanhSachHoaDon
    {
        int n;
        static List<HoaDon> DSHD;

        //khoi tao DS hoa don
        public DanhSachHoaDon()
        {
            DSHD=new List<HoaDon>();
        }

        //them moi hoa don vao danh sach
        public void ThemDSHD()
        {
            Console.Write("So luong hoa don: ");
            while (int.TryParse(Console.ReadLine(), out n) == false)
            {
                Console.Write("Nhap sai! Vui long nhap lai!");
            }
            for (int i = 0; i < n; i++)
            {
                HoaDon HD = new HoaDon();
                HD.NhapHD();
                DSHD.Add(HD);
                HD.ghiFile();
            }
        }

        //xuat danh sach hoa don
        public void XuatDSHD()
        {
            foreach (HoaDon xuat in DSHD)
            {
                xuat.XuatHD();
            }
        }

        //tim hoa don theo ma HD
        public HoaDon TimHD(string matim)
        {
            foreach (HoaDon tim in DSHD)
            {
                if (matim == tim.MaHoaDon)
                {
                    return tim;
                }
            }
            return null;
        }

        //xoa hoa don
        public bool XoaHD(string maxoa)
        {
            HoaDon Vtxoa = TimHD(maxoa);
            if (Vtxoa != null)
            {
                DSHD.Remove(Vtxoa);
                n--;
                return true;
            }
            return false;
        }

        //sua hoa don
        public void suaHoaDon(string maSua)
        {
            int chon = -1;
            HoaDon HD = TimHD(maSua);
            if (HD != null)
            {
                while (chon != 0)
                {
                    Console.Write("\nChon: \n\t1. Sua ma nhan vien\n\t2. Sua ma khach hang\n\t0. Quay lai");
                    while (int.TryParse(Console.ReadLine(), out chon) == false)
                    {
                        Console.Write("\nNhap sai! Vui long nhap lai");
                    }
                    switch (chon)
                    {
                        case 1:
                            Console.Write("\nNhap ma nhan vien can sua: ");
                            HD.MaNhanVien = Console.ReadLine();
                            break;
                        case 2:
                            Console.Write("\nNhap ma khach hang can sua: ");
                            HD.MaKhachHang = Console.ReadLine();
                            break;
                    }
                }
            }
            else
                Console.Write("\nKhong tim thay hoa don voi ma hoa don vua nhap");
        }
        //doc file
        public void docFile()
        {
            FileStream file = new FileStream("DSHoaDon.txt", FileMode.Open);
            BinaryReader br = new BinaryReader(file);
            while (br.PeekChar() != -1)
            {
                HoaDon HD = new HoaDon();
                HD.MaHoaDon = br.ReadString();
                HD.MaNhanVien = br.ReadString();
                HD.MaKhachHang = br.ReadString();
                HD.NgayLap = DateTime.Parse(br.ReadString());
                DSHD.Add(HD);
            }
            br.Close();
            file.Close();
        }
    }
}
